package com.example.httpdemo.net.token;

public class TokenManager {
    private static TokenManager instance;
    private String accessToken;
    private String refreshToken;
    private long expiresIn;
    private long tokenRefreshTime; // 提前5分钟刷新的时间
    
    private TokenManager() {}
    
    public static synchronized TokenManager getInstance() {
        if (instance == null) {
            instance = new TokenManager();
        }
        return instance;
    }
    
    public void updateTokens(String accessToken, String refreshToken, long expiresIn) {
        this.accessToken = accessToken;
        this.refreshToken = refreshToken;
        this.expiresIn = expiresIn;
        this.tokenRefreshTime = System.currentTimeMillis() + (expiresIn - 300) * 1000; // 提前5分钟
    }
    
    public String getAccessToken() {
        return accessToken;
    }
    
    public String getRefreshToken() {
        return refreshToken;
    }
    
    public boolean isTokenExpired() {
        return System.currentTimeMillis() >= tokenRefreshTime;
    }
    
    public synchronized void clearTokens() {
        accessToken = null;
        refreshToken = null;
        expiresIn = 0;
        tokenRefreshTime = 0;
    }
}